<template>
    <div class="login-container">
        <img src="../assets/bg.webp" class="login-bg" />
        <div class="login-box">

            <!-- 头像区域 -->
            <div class="text-center avatar-box">
                <img src="../assets/logo.webp" class="img-thumbnail avatar" alt="">
            </div>

            <!-- 表单区域 -->
            <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer">
                <div style="width:100%;height: 80px;">
                    <div style="position: absolute;width:100%;left: 0;">
                        <h3 class="loginTitle">
                            黑土对白云说 登录
                        </h3>
                    </div>
                </div>
                <el-form-item prop="username">
                    <el-input type="text" v-model="loginForm.username" placeholder="亲，请输入用户名">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" placeholder="亲，请输入密码">
                    </el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码">
                    </el-input>
                    <img :src="captchaUrl" @click="getCode" ref="code" style="position: absolute; top: 2px; right: 2px;">

                </el-form-item>
                <a href="/register" class="reg">注册</a>
                <el-button type="primary" style="width:100%" @click="submitLogin">登录</el-button>
            </el-form>

        </div>
    </div>
</template>
  
<script>
import request from '@/utils/request';
import qs from 'qs';
import { getToken, setToken, removeToken } from '@/utils/auth';

export default {
    name: 'Login',
    data() {
        return {
            captchaUrl: "/gpt/back/mp/kaptcha/img",
            loginForm: {
                username: "",
                password: "",
                code: ''
            },
            checked: true,
            rules: {
                username: [{ required: true, message: "请输入用户名", trigger: "blur" }, { min: 5, max: 14, message: '长度在 5 到 14 个字符', trigger: 'blur' }
                ],
                password: [{ required: true, message: "请输入密码", trigger: "blur" }, , { min: 6, message: '密码长度要大于6', trigger: 'blur' }],
                code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
            }

        }
    },
    methods: {
        getCode(){
      //点击刷新图形验证码
            this.$refs.code.src = this.captchaUrl + '?time='+ Date.now();
        },
        submitLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    //localhost:22346/gpt/back/mp/login/login
                    this.loading = true;
                    let params = {
                        username: this.loginForm.username,
                        password: this.loginForm.password,
                        code: this.loginForm.code,
                    };
                    request.post('/mp/login/login', qs.stringify(params)).then(res => {
                        if (res.state == 1 && res.data != null && res.data.token != -1) {
                            //登录成功
                            setToken(res.data.token);
                            this.$router.replace({ path: '/' });
                        } else {
                            this.$message.error(res.message);
                            this.getCode();
                        }
                    });
                } else {
                    this.$message.error('登录出错请重新输入');
                    return false;
                }
            });
        }
    }
};

</script>
  
<style lang="less" scoped>
.login-bg {
    width: 100%;
    position: absolute;
}

.login-container {
    background-color: #111214;
    height: 100%;
}

.login-box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .form-login {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        box-sizing: border-box;
    }

}

.form-control {
    flex: 1;
}

.avatar-box {
    position: absolute;
    width: 100%;
    top: -65px;
    left: 0;
    text-align: center;

    .avatar {
        width: 120px;
        height: 120px;
        border-radius: 50% !important;
        box-shadow: 0 0 6px #efefef;
    }
}

.reg {
    color: #917b7b;
    size: 12px;

}

.loginContainer {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 0 auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background: aliceblue;
    border: 1px solid blueviolet;
    box-shadow: 0 0 25px #f885ff;
}

.loginTitle {
    margin: 10px auto 30px auto;
    text-align: center;
    font-size: 32px;
}
</style>